package com.webshop.servlet;

import com.webshop.Dao.AdminDao;
import com.webshop.Dao.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/AdminServlet")
public class AdminServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        String method = req.getParameter("method");
        if (method.equals("all")){
            req.getSession().setAttribute("users", AdminDao.getUsers());
            resp.sendRedirect("a@shell.jsp");
            return;
        }
        if (method.equals("username")){
            req.getSession().setAttribute("users",AdminDao.searchUsername(req.getParameter("username")));
            resp.sendRedirect("a@shell.jsp");
        }
        if (method.equals("name")){
            req.getSession().setAttribute("users",AdminDao.searchName(req.getParameter("name")));
            resp.sendRedirect("a@shell.jsp");
        }
        if(method.equals("delete")){
            AdminDao.deleteUser(UserDao.getUser(Integer.parseInt(req.getParameter("id"))));
            resp.sendRedirect("a@shell.jsp");
        }
        if (method.equals("ban")){
            AdminDao.update("state","-1",Integer.parseInt(req.getParameter("id")));
            resp.sendRedirect("a@shell.jsp");
        }
        if (method.equals("active")){
            AdminDao.update("state","1",Integer.parseInt(req.getParameter("id")));
            resp.sendRedirect("a@shell.jsp");
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
